/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package xowl.interpreter.impl;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Laurent WOUTERS
 */
public class StatementBlockRunner extends StatementRunner {
    private xowl.lang.actions.Block statement;
    private List<xowl.lang.actions.Statement> innerStatements;
    private int nextToPush;

    public StatementBlockRunner(xowl.lang.actions.Block statement, ThreadRunner runner) {
        super(runner);
        this.statement = statement;
        this.innerStatements = new ArrayList<xowl.lang.actions.Statement>(statement.getAllContains());
        this.nextToPush = 0;
    }

    @Override public String getFile() { return statement.getFile(); }
    @Override public int getLine() { return statement.getLine(); }
    @Override public void execute() {
        if (nextToPush == innerStatements.size()) {
            runner.statementPop();
            runner.contextPop();
        } else {
            if (nextToPush == 0)
                runner.contextCreate();
            runner.statementPush(innerStatements.get(nextToPush));
            nextToPush++;
        }
    }
    @Override public boolean onBreak() {
        runner.contextPop();
        return true;
    }
}
